export function hierarchize(list, parent) {
  return list.forEach(item => {
    item.parent = parent;
    if (item.children) hierarchize(item.children, item);
  });
}


export function findNode(treeList, predicate) {
  for (let item of treeList) {
    if (predicate(item)) return item;
    else if (item.children) {
      let found = findNode(item.children, predicate);
      if (found) return found;
    }
  }
  return null;
}


export function findParents(node, parents = []) {
  const nodes = [node, ...parents];
  if (node.parent) return findParents(node.parent, nodes);
  return nodes;
}


export function getPaths(treeList, predicate) {
  const found = findNode(treeList, predicate);
  return found ? findParents(found) : [];
}



export function findPaths(treeList, predicate, paths=[]) {
  for (let item of treeList) {
    if (predicate(item)) return [...paths, item];
    else if (item.children) {
      const found = findPaths(item.children, predicate, [...paths, item]);
      if (found.length > paths.length) return found;
    }  
  }
  return paths;

}
